# -*- coding: utf-8 -*-
# @Time    : 20210609
# @Author  : 柿子
# @File    : tasks.py
#%%
import pandas as pd
from pandas.core.reshape.concat import concat
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["font.family"] = "Microsoft YaHei"
#%%
stu_f = pd.read_excel("./体测分数_女生.xls")
stu_m = pd.read_excel("./体测分数_男生.xls")
# %%


#%%
n = 3 # 分组数
cols = ["男1000米跑", "男引体"]
plt.figure(figsize=(14, 7))
for key in cols:
    # ------------------------- 计入0 -------------------------- #
    # t = stu_m[key].replace({0: np.nan})
    # b = [(t.max() - t.min()) * i / n + t.min() for i in range(n+1)]
    # b.insert(0,0)
    # m_t = pd.cut(stu_m[key], bins=b, right=True)
    # -------------------------不计入0-------------------------- #
    m_t = pd.cut(stu_m[key].replace({0: np.nan}), bins=n, right=True)
    mr_count = m_t.value_counts()
    ax = plt.subplot(1, len(cols), cols.index(key) + 1)
    plt.pie(
        x=mr_count,
        autopct="%.2f%%",
        pctdistance=0.8,
        wedgeprops={
            "linewidth": 5,
            "width": 0.4,
            "edgecolor": "white",
        },
        colors=["#ffc288", "#fea82f", "#ff6701"],
        textprops={"fontsize": 12, "color": "#fcecdd"},
    )
    plt.legend(
        mr_count.index,
        bbox_to_anchor=(
            0.5,
            0.45,
        ),
        loc="center",
        labelcolor="#ff6701",
        framealpha=0,
    )
    plt.title(label=key, fontdict={"size": 14, "color": "#ff6701"}, y=0.55)

# %% #女800米跑 # 女跳远
# stu_f[['女800米跑','女跳远']].replace({0:np.nan}).describe()
cols = ["女800米跑", "女跳远"]
t_stu_f = stu_f[cols].replace({0: np.nan})
t_stu_f["女800米跑"] = (
    t_stu_f["女800米跑"].map(lambda x: x if x < 10 else x / 1000).replace({1: np.nan})
)  # 每小时48公里这个优点太快了吧....
t_stu_f.describe()


plt.figure(figsize=(14, 7))
for c in cols:
    ax = plt.subplot(1, len(cols), cols.index(c) + 1)
    num, cut, fig = ax.hist(t_stu_f[c], bins=4)
    plt.xticks(ticks=cut)
    plt.box(False)
    plt.grid(axis="x")

    dics = dict(zip([(cut[i + 1] + v) / 2 for i, v in enumerate(cut[:-1])], num))
    for x, y in dics.items():
        plt.text(x=x, y=y + 3, s=y, ha="center")
    plt.title(c)


# %%
# -----------------------------预处理------------------------------ #
standard = dict(男=[0, 16.5, 23.3, 26.4, 100], 女=[0, 16.5, 22.8, 25.4, 100])
bin_label = ["低体重", "正常", "超重", "肥胖"]

stu_BMI = pd.DataFrame()
for i in [stu_m, stu_f]:
    t = i[["性别", "BMI"]]
    t["性别"] = t["性别"].map(lambda x: x.replace(" ", ""))
    gender = t["性别"].unique()[0]
    t["BMI_s"] = pd.cut(
        i["BMI"], bins=standard[gender], labels=["低体重", "正常", "超重", "肥胖"]
    )

    stu_BMI = pd.concat([stu_BMI, t])
stu_BMI["BMI"] = stu_BMI["BMI"].replace({0: np.nan})

stu_BMI["BMI_s"] = stu_BMI["性别"] + "-" + stu_BMI["BMI_s"].astype(str)

# -----------------------------数据准备------------------------------ #
colors = {
    "女-nan": "#fff1f4ff",
    "女-低体重": "#ffb9c8ff",
    "女-正常": "#ff96adff",
    "女-肥胖": "#ff446dff",
    "女-超重": "#a00023ff",
    "男-nan": "#bbe6ffff",
    "男-低体重": "#56c2ffff",
    "男-正常": "#00a3ffff",
    "男-肥胖": "#005a8dff",
    "男-超重": "#05486dff",
}

g_color = {"男": "#005a8d", "女": "#ff96ad"}

gender = stu_BMI["性别"].value_counts().sort_index()
groups = stu_BMI["BMI_s"].value_counts().sort_index()

# -----------------------------绘图------------------------------ #
plt.figure(figsize=(12, 12))
plt.pie(
    groups,
    # labels=groups.index,
    autopct="%.1f%%",
    radius=1,
    pctdistance=0.9,
    # labeldistance=0.92,
    # labels=groups.values,
    wedgeprops=dict(
        linewidth=4,
        width=0.2,
        edgecolor="w",
    ),
    colors=[colors[i] for i in groups.index],
    textprops={"fontsize": 9, "color":'w'},
)
plt.legend(groups.index, loc="center", ncol=2, bbox_to_anchor=(0.5, 0.45), framealpha=0)

plt.pie(
    gender,
    radius=0.8,
    autopct="%.1f%%",
    pctdistance=0.9,
    labels=gender.index,
    labeldistance=0.83,
    wedgeprops=dict(
        linewidth=4,
        width=0.2,
        edgecolor="w",
    ),
    textprops={"fontsize": 10, "color":'w'},
    colors=[g_color[i] for i in gender.index],
)

plt.title("性别BMI分布", y=0.55,fontdict={'size':18})

